<?

/****************************************
*** memcachedb_manage version 0.1 @ 2009-6-12
*** code by hqlulu#gmail.com
*** http://www.aslibra.com
*** http://code.google.com/p/memcachedb-manage/
*** http://code.google.com/p/memcachedb/
*** let the memcachedb acts like memcached!
****************************************/

class memcachedb extends Memcache{

	//是否有调试信息
	var $debug = false;
	//兼容使用memcache，这样就不用set时再保存额外数据
	var $is_memcachedb = true;
	//清理时间开始为0时，默认最小的清理时间差
	var $mini_clear_time = 86400;
	//临时数据前缀
	var $prefix = 'db_';
	//临时数据，清理的最后时间戳
	var $last_timeid_key = 'last_timeid';
	
	//本地处理缓存有效性
	var $cache_expire = 900;
	var $cache_prefix = 'WWW_FILE_';
	var $cache_time_prefix = 'WWW_TIME_';

	function memcachedb(){
	}
	
	//改写函数
	//此功能适合前端无法判断数据是否有效的情况，需要自行清理过期数据
	function set($key, $value, $flag=0, $expire=0){

		if($expire>0 && $this->is_memcachedb){
			$mykey = $this->prefix.(time() + $expire);
			//尝试取得自增值
			$i = parent::increment($mykey);
			if($i == FALSE){
				//尝试增加key，防止用set产生的冲突
				$i = parent::add($mykey,1) ? 1 : parent::increment($mykey);
			}
			parent::set($this->mykey($mykey, $i), $key);
		}

		return parent::set($key, $value, $flag, $expire);
	}

	//管理自增的键值组合方式
	function mykey($mykey, $i){
		return $mykey.'_'.$i;
	}

	//保存上次清理的时间戳的键值
	function last_timeid_key(){
		return $this->prefix.$this->last_timeid_key;
	}
	
	//清理
	function clear($start=0, $end=0){

		//取得上次清理的最后时间戳，可以不需要从time_start开始
		$last_timeid = parent::get($this->last_timeid_key());
		if($last_timeid == false) $last_timeid = 0;
		$start = $last_timeid>$start ? $last_timeid : $start;
		$start = $start>0 ? $start : ( time() - $this->mini_clear_time );
		$end = $end>0 ? $end : time();
		if($this->debug)echo " clear $start to $end <br>";

		for($mytime = $start; $mytime<=$end; $mytime++){
			$mykey = $this->prefix.$mytime;
			$auto_id = parent::get($mykey);
			if($auto_id){
				if($this->debug)echo "<hr>clear time: $mytime<br>";
				for($i = 1;$i<=$auto_id;$i++){
					$key = parent::get($this->mykey($mykey, $i));
					if($this->debug)echo " delete ".$k."<br>";
					parent::delete($key);
					if($this->debug)echo " delete ".$this->mykey($mykey, $i)."<br>";
					parent::delete($this->mykey($mykey, $i));
				}
				if($this->debug)echo " delete $mykey<br>";
				parent::delete($mykey);
			}
		}

		//记下当前处理过的时间戳
		$this->set($this->last_timeid_key(), $end);
	}

	//缓存读写
	//此功能适合PHP使用的数据，自行判断数据是否有效的情况
	function get_cache($key, $expire=0){
		$expire = $expire>0 ? $expire : 0;
		if($expire == 0)
			return parent::get($this->cache_prefix.$key);
		$cache_time = parent::get($this->cache_time_prefix.$key);
		$cache_time = $cache_time == FALSE ? 0 : $cache_time;
		if(time() - $cache_time <= $expire)
			return parent::get($this->cache_prefix.$key);
		return FALSE;
	}

	function set_cache($key, $value, $flag=0, $expire=0){
		$expire = $expire>=0 ? $expire : $this->cache_expire;
		parent::set($this->cache_time_prefix.$key, time());
		return parent::set($this->cache_prefix.$key, $value, $flag, $expire);
	}
}
?>